﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Presentation
{
    public partial class DanhSachLop : Form
    {
        public DanhSachLop()
        {
            InitializeComponent();
        }

        private void DanhSachLop_Load(object sender, EventArgs e)
        {
            var da = new QuanLyHocSinhDataSetTableAdapters.KhoiTableAdapter();
            comboKhoi.DataSource = da.GetData();
            comboKhoi.DisplayMember = "tenkhoi";
            comboKhoi.ValueMember = "makhoi";

            loadlop();
        }

        private void loadlop()
        {
            var lopda = new QuanLyHocSinhDataSetTableAdapters.LopTableAdapter();
            var dt = lopda.GetDataByMaKhoi((int)comboKhoi.SelectedValue);
            if (dt.Rows.Count > 0)
            {
                comboLop.DataSource = dt;
                comboLop.ValueMember = "malop";
                comboLop.DisplayMember = "tenlop";
            }
            else
            {
                comboLop.DataSource = null;
            }
        }

        private void comboKhoi_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboKhoi.Focused)
            {
                loadlop();                
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboLop.SelectedIndex >= 0)
            {
                var da = new QuanLyHocSinhDataSetTableAdapters.HocSinhTableAdapter();
                var ds = new QuanLyHocSinhDataSet();
                da.Fill(ds.HocSinh);

                var lopda = new QuanLyHocSinhDataSetTableAdapters.LopTableAdapter();
                lopda.Fill(ds.Lop);

                reportDanhSachLop report = new reportDanhSachLop();
                report.Load(@"reportDanhSachLop.rpt");
                report.SetDataSource(ds);
                report.SetParameterValue("malopParam", (int)comboLop.SelectedValue);
                if (report.HasRecords)
                {
                    crystalReportViewer1.ReportSource = report;
                }
                else
                {
                    MessageBox.Show("Không có dữ liệu", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
